//
//  BaseViewController.m
//  QRMaster
//
//  Created by myApple on 15/11/9.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "BaseViewController.h"
#import "CommunityInfoLogic.h"
#import "ChineseString.h"
#import "pinyin.h"

@interface BaseViewController ()
{
    MBProgressHUD * m_progressHUD;
    CommunityInfoLogic * timeStamp;
}

@end

@implementation BaseViewController


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    timeStamp = [[CommunityInfoLogic alloc]init];
    self.view.backgroundColor=[UIColor colorWithRed:245/255.0
                                              green:245/255.0
                                               blue:245/255.0
                                              alpha:1.0];

    self.automaticallyAdjustsScrollViewInsets = NO;
}

#pragma mark ------------添加和取消等待条
- (void)stopWaiting
{
    [m_progressHUD hide:YES];
    [m_progressHUD removeFromSuperview];
    m_progressHUD = nil;
}


-(void)startWaiting:(NSString *)text color:(UIColor *)color addToView:(UIView *)view
{
    [self stopWaiting];
    m_progressHUD = [[MBProgressHUD alloc]init];
    m_progressHUD.labelText = text ;
    m_progressHUD.labelColor = color;
    [view addSubview:m_progressHUD];
    [m_progressHUD show:YES];
}

#pragma mark ----------更新时间戳和获取时间戳
-(NSString *)getTimestamp:(NSString *)communityId timeKey:(NSString *)timeKey
{
    timeStamp = [[CommunityInfoLogic alloc]init];
    NSString * str = [timeStamp getTimestamp:communityId timeKey:timeKey];
    return str;
}

-(void)updateTimestamp:(NSString *)communityId  timeKey:(NSString *)timeKey timeKeyValue:(NSString *)timeKeyValue
{
    timeStamp = [[CommunityInfoLogic alloc]init];
    [timeStamp updateTimestamp:communityId timeKey:timeKey timeKeyValue:timeKeyValue];
}


//触摸关闭键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

//判断是否有网络
-(BOOL)IsHaveNetwork
{
    Reachability * reach = [Reachability reachabilityForInternetConnection];
    if([reach currentReachabilityStatus] == NotReachable) {
        [PromptInfoView showMessage:NSLocalizedString(@"noNetWork", nil)];
        return YES;
    }
    return NO;
}

//删除多余表格
-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *view = [UIView new];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    
}

#pragma mark -----字符串首字母获取
-(NSString *)firstChar:(NSString *)nameStr
{
    //创建一个临时的数据模型对象
    ChineseString *chineseString=[[ChineseString alloc]init];
    NSString *pinYinResult = [NSString string];
    chineseString.string = nameStr;
    if(chineseString.string==nil)
    {
        chineseString.string=@"";
    }
    if(![chineseString.string isEqualToString:@""])
    {
        //join(链接) the pinYin (letter字母) 链接到首字母
        char FirstHeader = pinyinFirstLetter([chineseString.string characterAtIndex:0]);
        if ((FirstHeader < 91 && FirstHeader > 64)||(FirstHeader < 123 && FirstHeader > 96)) {
            pinYinResult = [pinYinResult stringByAppendingString:[[NSString stringWithFormat:@"%c",FirstHeader]uppercaseString]];
        }
        else
        {
            pinYinResult = @"#";
        }
    }
    return  pinYinResult;
}


// @“#”转换
-(NSString *)FirstCharToStr:(NSString *)str
{
    NSString *returnStr;
    if(str!=nil &&str.length!=0)
    {
        char FirstHeader = [str characterAtIndex:0];
        if ((FirstHeader < 91 && FirstHeader > 64)||(FirstHeader < 123 && FirstHeader > 96)) {
            returnStr=str;
        }
        else
        {
            returnStr=@"#";
        }
    }
    else
    {
        if (![str isEqualToString:@""]) {
            returnStr = @"#";
        }
        else
        {
            returnStr = @"";
        }
        
    }
    
    return returnStr;
}

@end
